package com.boot.core.thread;

import javax.annotation.Nullable;
import java.util.HashMap;
import java.util.Map;

public class ThreadLocalHelper {

    /**
     * @author 霜寒 <1621856595@qq.com>
     * @description 线程的本地变量
     * @date 2019/8/27 11:49
     **/
    private final static ThreadLocal<Map<String, Object>> HOLDER = new ThreadLocal<>();

    /**
     * @author 霜寒 <1621856595@qq.com>
     * @description 存储
     * @date 2019/8/27 11:49
     **/
    public static void setValue(String key, Object value) {
        Map<String, Object> map = HOLDER.get();
        if (null == map) {
            map = new HashMap<>(1);
            map.put(key, value);
            HOLDER.set(map);
        } else {
            map.put(key, value);
        }
    }

    /**
     * @author 霜寒 <1621856595@qq.com>
     * @description 获取
     * @date 2019/8/27 11:50
     **/
    @Nullable
    public static Object getValue(String key) {
        Map<String, Object> map = HOLDER.get();
        if (null == map) {
            return null;
        } else {
            return map.get(key);
        }
    }

    /**
     * @author 霜寒 <1621856595@qq.com>
     * @description 获取
     * @date 2019/8/27 11:50
     **/
    @Nullable
    public static Map<String, Object> getValue() {
        return HOLDER.get();
    }

    /**
     * @author 霜寒 <1621856595@qq.com>
     * @description 清除
     * @date 2019/8/27 11:50
     **/
    public static void clear() {
        HOLDER.remove();
    }

}
